using System;
using System.Collections;
using UnityEngine;

namespace Framework
{
    public class AssetLoadResult : IAssetResult, IEnumerator
    {
        private static int _id = 1;
        public int InstanceId { get; private set; }
        public string Path { get; private set; }
        public bool IsDone { get; private set; }
        public object Asset { get; private set; }
        public AsyncOperation SceneAsset { get; private set; }

        /// <summary>
        /// 是一个GameObject,加载时会克隆一份
        /// </summary>
        public bool IsGameObject { get; set; }
        public bool SceneSingleMode { get; set; }

        private Action<IAssetResult> _callback;
        private bool _sceneAllowActivation;
        private bool _sceneActive;
        public void SetSceneAllowActivation(bool allowSceneActivation)
        {
            _sceneAllowActivation = allowSceneActivation;
            if (!IsDone)
            {
                return;
            }
            if (SceneAsset == null)
            {
                return;
            }
            if (SceneAsset != null)
            {
                SceneAsset.allowSceneActivation = _sceneAllowActivation;
            }
        }
        public void SetSceneActive()
        {
            _sceneActive = true;
            if (!IsDone) return;
            var scene = UnityEngine.SceneManagement.SceneManager.GetSceneByPath(Path);
            if (scene == null)
            {
                return;
            }
            if (!scene.isLoaded)
            {
                return;
            }
            UnityEngine.SceneManagement.SceneManager.SetActiveScene(scene);
        }
        public void SetCompleteCallback(Action<IAssetResult> callback)
        {
            _callback = callback;
        }
        public void Begin(string path)
        {
            IsDone = false;
            if (InstanceId < 1)
            {
                InstanceId = _id++;
            }
            this.Asset = null;
            this.SceneAsset = null;
            this.Path = path;
            IsGameObject = path.EndsWith("prefab");
            _sceneAllowActivation = true;
            SceneSingleMode = false;
            _sceneActive = false;
        }
        public void Finish(object asset)
        {
            this.Asset = asset;
            SetDone();
        }
        public void Finish(AsyncOperation sceneAsset)
        {
            this.Asset = sceneAsset;
            this.SceneAsset = sceneAsset;
            if (SceneAsset != null)
            {
                SceneAsset.allowSceneActivation = _sceneAllowActivation;
            }
            if (_sceneAllowActivation)
            {
                //如果加载后激活就等切换完成
                return;
            }
            SetDone();
        }
        public void Finish()
        {
            SetDone();
        }
        public void SceneFinish()
        {
            if (_sceneActive)
            {
                SetSceneActive();
            }
            SetDone();
        }
        private void SetDone()
        {
            if (IsDone)
            {
                return;
            }
            this.IsDone = true;
            if (_callback != null)
            {
                var callback = _callback;
                _callback = null;
                callback.Invoke(this);
            }
        }

        public object Current => Asset;
        public bool MoveNext()
        {
            return !IsDone;
        }

        public void Reset()
        {

        }
    }
}